using System;
using System.Collections.Generic;
using System.Xml;
using System.Data;
using System.Drawing;
using System.Text;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace Tes
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            keyValue = textBox1.Text;
            label1.Text = RCVEncryptingPassword(textBox2.Text);
        }
        private string keyValue = "RCV";
        public string Key
        {
            set { keyValue = value; }
            get { return keyValue; }
        }

        public string RCVEncryptingPassword(string password)
        {
            string encryptPassword = password + keyValue;
            byte[] passwordBytes = Encoding.UTF8.GetBytes(encryptPassword);

            HashAlgorithm hash = new MD5CryptoServiceProvider();
            byte[] hashBytes = hash.ComputeHash(passwordBytes);

            encryptPassword = Convert.ToBase64String(passwordBytes);
            return encryptPassword;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            keyValue = "RCV";
            label4.Text = RCVEncryptingPassword(textBox2.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            xmlFilePath = Application.StartupPath + @"\App.Config";
            label5.Text = ReadNodeValue("connectionStrings");
        }

        private string xmlFilePath = "App.Config";
        public string XmlFilePath
        {
            set { xmlFilePath = value; }
            get { return xmlFilePath; }
        }

        private string databaseName = "RCVDB2000WDN1";
        public string DatabaseName
        {
            set { databaseName = value; }
            get { return databaseName; }
        }   
        
        void AddNewNode(         
        string nodeName, string nameValue,
        string attributeName, string attributeValue)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(xmlFilePath);
            XmlElement newXmlElement =
                    xmlDocument.CreateElement("add");
            newXmlElement.SetAttribute("name", nameValue);
            newXmlElement.SetAttribute(attributeName, attributeValue);
            xmlDocument.DocumentElement[
            nodeName].AppendChild(newXmlElement);             
            xmlDocument.Save(xmlFilePath);
        }
    
        bool DeleteNodeName(string nodeName)
        {
            bool foundNode = false;
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(xmlFilePath);
            XmlNodeList xmlNodeList =
            xmlDocument.GetElementsByTagName(nodeName);
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                foreach (XmlNode xmlElement in
                    xmlNode.ChildNodes)
                {
                    if (xmlElement.LocalName == "add" &&
                    xmlElement.Attributes["name"].Value == databaseName)
                    {
                        foundNode = true;
                        xmlNode.RemoveChild(xmlElement);                        
                        break;
                    }
                }
                if(foundNode) break;
            }
            xmlDocument.Save(xmlFilePath);
            return foundNode;
        }
        string ReadNodeValue(string nodeName)
        {
            string nodeValue = "";
            bool foundNode = false;
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load(xmlFilePath);

            XmlNodeList xmlNodeList =
            xmlDocument.GetElementsByTagName(nodeName);
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                foreach (XmlElement xmlElement in
                xmlNode.ChildNodes)
                {
                    if (xmlElement.LocalName == "add" &&
                    xmlElement.Attributes["name"].Value == databaseName)
                    {
                        nodeValue = xmlElement.Attributes["connectionString"].Value;
                        foundNode = true;
                        break;
                    }
                }
                if(foundNode) break;
            }
            return nodeValue;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            xmlFilePath = Application.StartupPath + @"\App.Config";
            DeleteNodeName("connectionStrings");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            xmlFilePath = Application.StartupPath + @"\App.Config";
            AddNewNode("connectionStrings","RCVDB","connectionString","ASAS");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            LogTraceProvider cls = new LogTraceProvider();
            cls.WriteErrorMessageToLogFile("Test KhangANH");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Class1 cls = new Class1();
            cls.ExecuteNonQuery();
        }

        private void button8_Click(object sender, EventArgs e)
        {
             label1.Text = RCVDecPwd(label4.Text );
        }
        public string RCVDecPwd(string password)
        {
            string originalPassword = "";
            byte[] inputByteArray = Convert.FromBase64String(password);

            originalPassword = Encoding.UTF8.GetString(inputByteArray);

            return originalPassword.Substring(0, originalPassword.Length-keyValue.Length);;
        }
    }
}